package com.jiangjiang.eshop.auth.facade;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiangjiang.eshop.auth.cache.PriorityCacheManager;
import com.jiangjiang.eshop.auth.convert.AccountConvert;
import com.jiangjiang.eshop.auth.convert.PriorityConvert;
import com.jiangjiang.eshop.auth.domain.AccountEntity;
import com.jiangjiang.eshop.auth.dto.AccountDTO;
import com.jiangjiang.eshop.auth.dto.AccountQueryPageDTO;
import com.jiangjiang.eshop.auth.service.AccountPriorityRelationShipService;
import com.jiangjiang.eshop.auth.service.AccountService;
import com.jiangjiang.eshop.auth.service.PriorityService;
import com.jiangjiang.eshop.auth.service.RolePriorityRelationShipService;
import com.jiangjiang.eshop.common.help.DateHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName AccountFacade
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/7 5:27
 * @Version 1.0
 **/
@Slf4j
@Component
public class AccountFacade {
    @Autowired
    private AccountService accountService;
    @Autowired
    private AccountConvert accountConvert;
    @Autowired
    private AccountRoleRelationshipFacade accountRoleRelationshipFacade;
    @Autowired
    private AccountPriorityRelationshipFacade accountPriorityRelationshipFacade;
    @Autowired
    private PriorityCacheManager priorityCacheManager;

    /**
     * 分页查询用户账户数据
     *
     * @param dto 查询条件
     * @return 查询结果
     */
    public Page<AccountDTO> queryForPage(AccountQueryPageDTO dto) {
        Page<AccountDTO> page = accountConvert.convertAccountPage2DTO(accountService.queryForPage(dto));
        page.getRecords().forEach(accountDTO -> {
            accountDTO.setRoleRelations(accountRoleRelationshipFacade.queryRoleRelationsByAccountId(accountDTO.getId()));
            accountDTO.setPriorityRelations(accountPriorityRelationshipFacade.queryPriorityRelationsByAccountId(accountDTO.getId()));
        });
        return page;
    }

    /**
     * 根据账户id查询账户数据
     *
     * @param accountId 账户id
     * @return 查询结果
     */
    public AccountDTO queryAccountById(Long accountId) {
        AccountDTO accountDTO = accountConvert.convertEntity2DTO(accountService.queryAccountById(accountId));
        // 补充关联数据
        accountDTO.setRoleRelations(accountRoleRelationshipFacade.queryRoleRelationsByAccountId(accountId));
        accountDTO.setPriorityRelations(accountPriorityRelationshipFacade.queryPriorityRelationsByAccountId(accountId));
        return accountDTO;
    }

    /**
     * 用户账号保存
     *
     * @param account 用户账号数据
     * @return 处理结果
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveAccount(AccountDTO account) {
        AccountEntity entity = accountConvert.convertDTO2Entity(account);
        accountService.save(entity);
        // 保存账号的角色的关联关系
        accountRoleRelationshipFacade.saveRelationShip(account.getRoleRelations());
        // 保存账号和权限的关联关系
        accountPriorityRelationshipFacade.saveRelationShip(account.getPriorityRelations());

        return true;
    }

    /**
     * 用户账号更新
     *
     * @param account
     * @return
     */
    public Boolean accountUpdate(AccountDTO account) {
        AccountEntity entity = accountConvert.convertDTO2Entity(account);
        accountService.updateById(entity);
        // 保存账号的角色的关联关系
        accountRoleRelationshipFacade.relationShipUpdate(account.getRoleRelations());
        // 保存账号和权限的关联关系
        accountPriorityRelationshipFacade.relationShipUpdate(account.getPriorityRelations());

        // 清空缓存数据
        priorityCacheManager.removeCache(account.getId());

        return true;
    }


    /**
     * 删除用户账户
     *
     * @param accountId
     * @return 处理结果
     */
    public Boolean deleteById(Long accountId) {
        boolean result = accountService.removeById(accountId);
        if (result) {
            // 清空缓存数据
            priorityCacheManager.removeCache(accountId);
        }
        return result;
    }

    /**
     * 用户账户密码更新
     *
     * @param account 用户账户
     * @return 处理结果
     */
    public Boolean updatePassword(AccountDTO account) {
        return accountService.updatePassword(account.getId(), account.getPassword());
    }
}
